import sqlite3
from datetime import datetime

# 连接到数据库
conn = sqlite3.connect('./openmemory.db')
cursor = conn.cursor()

print("=== 当前数据库状态验证 ===")

# 1. 检查所有应用
cursor.execute("SELECT id, name, created_at FROM apps ORDER BY created_at")
apps = cursor.fetchall()
print(f"\n1. 发现 {len(apps)} 个应用:")
for app in apps:
    cursor.execute("SELECT COUNT(*) FROM memories WHERE app_id = ?", (app[0],))
    count = cursor.fetchone()[0]
    print(f"   - ID: {app[0]}")
    print(f"   - 名称: {app[1]}")
    print(f"   - 创建时间: {app[2]}")
    print(f"   - 记忆数量: {count}")
    print()

# 2. 检查所有记忆总数
cursor.execute("SELECT COUNT(*) FROM memories")
total_memories = cursor.fetchone()[0]
print(f"2. 总记忆数量: {total_memories}")

# 3. 检查记忆分布
cursor.execute("""
    SELECT a.name, COUNT(m.id) as memory_count
    FROM apps a
    LEFT JOIN memories m ON a.id = m.app_id
    GROUP BY a.id, a.name
    ORDER BY memory_count DESC
""")
distribution = cursor.fetchall()
print(f"\n3. 记忆分布:")
for app_name, count in distribution:
    print(f"   {app_name}: {count} 条记忆")

# 4. 检查最新的5条记忆
cursor.execute("""
    SELECT m.id, m.content, a.name as app_name, m.created_at
    FROM memories m
    JOIN apps a ON m.app_id = a.id
    ORDER BY m.created_at DESC
    LIMIT 5
""")
latest_memories = cursor.fetchall()
print(f"\n4. 最新的5条记忆:")
for memory in latest_memories:
    print(f"   - ID: {memory[0]}")
    print(f"   - 应用: {memory[1]}")
    print(f"   - 内容预览: {memory[2][:100]}...")
    print(f"   - 时间: {memory[3]}")
    print()

conn.close()

print("=== 验证完成 ===")
print("当前状态：")
print("- 只有一个 'openmemory' 应用")
print("- 该应用包含 18 条记忆")
print("- n8n 和前端都应该看到这个应用")